//
//  MyShowDetailViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-30.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyShowDetailViewController.h"
#import "AppDelegate.h"
#import "ASIFormDataRequest.h"
#import "MainViewController.h"

@implementation MyShowDetailViewController
@synthesize data;
@synthesize detailView;
@synthesize imageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [data release];
    [detailView release];
    [imageView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)showMessge:(NSString *)message {
	UIAlertView *infoView = [[UIAlertView alloc] 
							 initWithTitle:@""
							 message:message
							 delegate:nil 
							 cancelButtonTitle:[AppDelegate getLanguageString:@"common_button_close"]
							 otherButtonTitles:nil];
	[infoView show];
	[infoView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    detailView.text = [NSString stringWithFormat:@"%@ %@ %@\n%@ %@", data.nickname, [AppDelegate getLanguageString:@"show_label_shot"], [data.createDate substringWithRange:NSMakeRange(0, 10)], [AppDelegate getLanguageString:@"show_label_view"], data.views];
    [imageView loadImage:data.bigImage andRequestQueue:requestQueue];
    
    if (data.alreadyView != 1) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/ShowViewAdd.ashx", [AppDelegate serviceUrl]]];
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
        [request addPostValue:data.showID forKey:@"txt_ShowID"];
        //[request addPostValue:phone forKey:@"txt_IP"];
        [request addPostValue:@"iphone" forKey:@"txt_ClientType"];
        [request addPostValue:[AppDelegate getLanguage] forKey:@"txt_Language"];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestComplete:)];
        [request setDidFailSelector:@selector(requestFailed:)];
        [requestQueue addOperation:request];
        [request release];
        //NSLog(@"post ShowViewAdd url = %@", url);
    }
}

- (void)viewDidAppear:(BOOL)animated {
    MainViewController *mainView = (MainViewController *)mainViewController;
    mainView.backView.frame = CGRectMake(284, 399, 26, 26);
}

- (void)viewDidDisappear:(BOOL)animated {
    MainViewController *mainView = (MainViewController *)mainViewController;
    mainView.backView.frame = CGRectMake(284, 424, 26, 26);
}

- (void)requestComplete:(ASIFormDataRequest *)request {
    NSString *result = [request responseString];
    //NSLog(@"Regist Failed! result=%@", result);
    if ([result rangeOfString:@"html"].length > 0) {
		//[self showMessge:@"Regist Failed!"];
        NSLog(@"Post ShowViewAdd Request Failed! result=%@", result);
        return;
    }
    
	//NSArray *resultInfos = [result componentsSeparatedByString:@"|"];
    //NSString *resultCode = [resultInfos objectAtIndex:0];
    //NSString *resultMessage = [resultInfos objectAtIndex:1];
	if ([@"1" isEqualToString:result]) {
        data.alreadyView = 1;
        data.views = [NSString stringWithFormat:@"%d", [data.views intValue] + 1];
        [data save];
        NSLog(@"Post ShowViewAdd Request Success! result=%@", result);
    } else {
        NSLog(@"Post ShowViewAdd Request Failed! result=%@", result);
    }
}

- (void)requestFailed:(ASIFormDataRequest *)request {
    NSLog(@"Post ShowViewAdd Request Failed! error=%@", [request error]);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.detailView = nil;
    self.imageView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
